#include "calculator.h"
#include "ui_calculator.h"
Calculator::Calculator(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Calculator)
{
    ui->setupUi(this);
    on_tb_clear_clicked();
    m_model = 0;
}
Calculator::~Calculator()
{
    delete ui;
}
void Calculator::on_tb_num0_clicked()
{
    clickedNumber(ui->tb_num0->text());
}
void Calculator::on_tb_num1_clicked()
{
    clickedNumber(ui->tb_num1->text());
}
void Calculator::on_tb_num2_clicked()
{
    clickedNumber(ui->tb_num2->text());
}
void Calculator::on_tb_num3_clicked()
{
    clickedNumber(ui->tb_num3->text());
}
void Calculator::on_tb_num4_clicked()
{
    clickedNumber(ui->tb_num4->text());
}
void Calculator::on_tb_num5_clicked()
{
    clickedNumber(ui->tb_num5->text());
}
void Calculator::on_tb_num6_clicked()
{
    clickedNumber(ui->tb_num6->text());
}
void Calculator::on_tb_num7_clicked()
{
    clickedNumber(ui->tb_num7->text());
}
void Calculator::on_tb_num8_clicked()
{
    clickedNumber(ui->tb_num8->text());
}
void Calculator::on_tb_num9_clicked()
{
    clickedNumber(ui->tb_num9->text());
}
void Calculator::clickedNumber(const QString &t)
{
    m_value += t;
    m_expretion += t;
    ui->lb_expretion->setText(m_expretion);
}
void Calculator::on_tb_add_clicked()
{
    middleResult();
    m_operator = FLAG_OPERATOR_ADD;
    m_expretion += ui->tb_add->text();
    ui->lb_expretion->setText(m_expretion);
}
void Calculator::on_tb_mul_clicked()
{
    middleResult();
    m_operator = FLAG_OPERATOR_MUL;
    m_expretion += ui->tb_mul->text();
    ui->lb_expretion->setText(m_expretion);
}
void Calculator::on_tb_div_clicked()
{
    middleResult();
    m_operator = FLAG_OPERATOR_DIV;
    m_expretion += ui->tb_div->text();
    ui->lb_expretion->setText(m_expretion);
}
void Calculator::on_tb_sub_clicked()
{
    middleResult();
    m_operator = FLAG_OPERATOR_SUB;
    m_expretion += ui->tb_sub->text();
    ui->lb_expretion->setText(m_expretion);
}
void Calculator::middleResult()
{
    if(m_firstOperator)
    {
        m_firstOperator = false;
        m_leftValue = m_value;
        m_value.clear();
    }else
    {
        m_rightValue = m_value;
        m_value.clear();
        switch (m_operator) {
        case FLAG_OPERATOR_ADD:
            m_leftValue = QString::number(m_leftValue.toInt() + m_rightValue.toInt());
            on_tb_equ_clicked();
            break;
        case FLAG_OPERATOR_SUB:
            m_leftValue = QString::number(m_leftValue.toInt() - m_rightValue.toInt());
            on_tb_equ_clicked();
            break;
        case FLAG_OPERATOR_MUL:
            m_leftValue = QString::number(m_leftValue.toInt() * m_rightValue.toInt());
            on_tb_equ_clicked();
            break;
        case FLAG_OPERATOR_DIV:
            if(m_rightValue == "0")
            {
                Calculator::on_tb_clear_clicked();
                ui->lb_result->setText("除数不能为0");
            }
            else
            {
                m_leftValue = QString::number(m_leftValue.toInt() / m_rightValue.toInt());
                on_tb_equ_clicked();
            }
            break;
        default:
            break;
        }
    }
}
void Calculator::on_tb_equ_clicked()
{
    m_rightValue = m_value;
    m_value.clear();
    switch (m_operator) {
    case FLAG_OPERATOR_ADD:
        m_result = QString::number(m_leftValue.toInt() + m_rightValue.toInt());
        break;
    case FLAG_OPERATOR_MUL:
        m_result = QString::number(m_leftValue.toInt() * m_rightValue.toInt());
        break;
    case FLAG_OPERATOR_DIV:
        m_result = QString::number(m_leftValue.toInt() / m_rightValue.toInt());
        break;
    case FLAG_OPERATOR_SUB:
        m_result = QString::number(m_leftValue.toInt() - m_rightValue.toInt());
        break;
    default:
        break;
    }
    ui->lb_result->setText(m_result);
}
void Calculator::on_tb_clear_clicked()
{
    m_firstOperator = true;
    m_operator = FLAG_OPERATOR_NONE;
    m_value.clear();
    m_expretion.clear();
    m_result.clear();
    m_leftValue.clear();
    m_rightValue.clear();
    ui->lb_expretion->setText("0");
    ui->lb_result->setText("0");
}
